﻿#region

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataInfo;
using KenLib.BusinessManager;

#endregion

public partial class Org_frmO_AppCompList : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((Org_MasterPage) Master).changeTitle("所属組合員企業一覧");
        ((Org_MasterPage) Master).HideLiArea("frmO_AppCompList");
        Page.Title = "所属組合員企業一覧";
        if (!IsPostBack)
        {
            rqf_ddl_組合選択.ErrorMessage = getError("ER00007");

            frm_AppCompListManager frmapplist = new frm_AppCompListManager();
            if (Convert.ToInt32(Session["役"]) != 1)
            {
                ddl_組合選択.Visible = false;
                txt_SearchValue.Focus();
                if (Session["企業コード_2"] != null)
                {
                    if (Session["lst_OR00027F"] != null)
                    {
                        loadSessionToForm();
                    }
                    else
                    {
                        List<本社情報> list = frmapplist.GetAllData(Convert.ToInt32(Session["企業コード_2"].ToString()));
                        DtTableToSession(list);
                        loadSessionToForm();
                    }
                }
                else
                {
                    Response.Redirect("~/frmD_Login.aspx");
                }
            }
            else
            {
                ddl_組合選択.Focus();
                List<本社情報> list = frmapplist.Select本社情報_Luan();
                ddl_組合選択.Items.Add(new ListItem("[選択]", "-1"));
                foreach (本社情報 obj本社情報 in list)
                {
                    ddl_組合選択.Items.Add(new ListItem(obj本社情報.企業名, obj本社情報.企業ID.ToString()));
                }
                if (Session["lst_OR00027F"] != null)
                {
                    loadSessionToForm();
                }
                else
                {
                    AppCode_Common.ShowEmtyGrid<本社情報>(grv_human, 5, "データがありません。", "");
                }
            }
        }
    }

    /// <summary>
    /// Gets the error.
    /// </summary>
    /// <param name="errorid">The errorid.</param>
    /// <returns></returns>
    protected string getError(string errorid)
    {
        ErrorManager errorManager = new ErrorManager();
        Error error = new Error();
        error.ErrorID = errorid;
        return errorManager.getError(error).Rows[0]["ERROR_CONTENT_JP"].ToString();
    }

    protected void btn_Search_Click(object sender, EventArgs e)
    {
        grv_human.Visible = true;
        frm_AppCompListManager frmapplist = new frm_AppCompListManager();
        string search = txt_SearchValue.Text.Replace("'", "''");
        if (search.Length > 1)
        {
            search = search[0].ToString() != "*" && search[search.Length - 1].ToString() != "*"
                         ? ("%" + search + "%")
                         : search;
            search = search[0].ToString() == "*" ? ("%" + search.Substring(1, search.Length - 1)) : search;
            search = search[search.Length - 1].ToString() == "*"
                         ? (search.Substring(0, search.Length - 1) + "%")
                         : search;
        }
        if (Convert.ToInt32(Session["役"]) != 1)
        {
            string query = search != ""
                               ? "AND (企業名 LIKE N'" + search + "' OR ＴＥＬ LIKE N'" + search + "'  OR 住所 LIKE N'" + search +
                                 "' OR 担当１氏名 LIKE N'" + search + "')"
                               : "";
            List<本社情報> list = frmapplist.GetDataWithSearchLike(Convert.ToInt32(Session["企業コード_2"].ToString()), query);
            DtTableToSession(list);
            loadSessionToForm();
            txt_SearchValue.Focus();
        }
        else
        {
            //grv_human.Visible = false;
            //GridView1.Visible = true;
            //AppCode_Common.ShowEmtyGrid<本社情報>(GridView1, 5, "データがありません。", "");
            string query = search != ""
                               ? "AND (企業名 LIKE N'" + search + "' OR ＴＥＬ LIKE N'" + search + "'  OR 住所 LIKE N'" + search +
                                 "' OR 担当１氏名 LIKE N'" + search + "')"
                               : "";
            List<本社情報> list = frmapplist.GetDataWithSearchLike(Convert.ToInt32(ddl_組合選択.SelectedValue), query);
            DtTableToSession(list);
            loadSessionToForm();
            ddl_組合選択.Focus();
        }
    }

    /// <summary>
    /// Dts the table to session.
    /// </summary>
    /// <param name="lst">The LST.</param>
    private void DtTableToSession(List<本社情報> lst)
    {
        Session["lst_OR00027F"] = lst;
        Session["ddl_OR00027F"] = ddl_組合選択.SelectedValue;
        Session["txt_OR00027F"] = txt_SearchValue.Text;
    }

    /// <summary>
    /// Loads the session to form.
    /// </summary>
    private void loadSessionToForm()
    {
        if (Session["lst_OR00027F"] != null)
        {
            List<本社情報> list = (List<本社情報>) Session["lst_OR00027F"];
            if (list.Count > 0)
            {
                grv_human.DataSource = list;
                grv_human.DataBind();
            }
            else
            {
                AppCode_Common.ShowEmtyGrid<本社情報>(grv_human, 5, "データがありません。", "");
            }
        }
        if (Session["ddl_OR00027F"] != null)
        {
            ddl_組合選択.SelectedValue = Session["ddl_OR00027F"].ToString();
        }
        if (Session["txt_OR00027F"] != null)
        {
            txt_SearchValue.Text = Session["txt_OR00027F"].ToString();
        }
    }
}